//# Copyright 2009,2007 Adam Geras
//# Distributed under the BSD License (See accompanying file license.txt or copy at
//# http://www.opensource.org/licenses/bsd-license.php)

namespace PsAccountManager
{
    /// <summary>
    /// A register for financial transactions.  Based on the BDD samples posted by Dan North.
    /// The intent is to demonstrate using BDD as implemented in Powershell.
    /// </summary>
    public class Account
    {
        private string _accountType;
        private double _accountBalance;

        /// <summary>
        /// Held private since not intended to be used.
        /// </summary>
        private Account()
        {
        }

        /// <summary>
        /// Creates an account of the specified type and with the specified balance.
        /// </summary>
        /// <param name="pAccountType">The account type</param>
        /// <param name="pInitialBalance">The initial balance</param>
        public Account(string pAccountType, double pInitialBalance):this()
        {
            if (string.IsNullOrEmpty(pAccountType)) throw new PsAccountManagerException(PsAccountManagerException.NullAccountType);
            AccountType = pAccountType;
            Balance = pInitialBalance;
        }

        public string AccountType
        {
            get { return _accountType; }
            set { _accountType = value; }
        }

        public double Balance
        {
            get { return _accountBalance; }
            set { _accountBalance = value; }
        }

        /// <summary>
        /// Transfers financial units from self to the specified account
        /// </summary>
        /// <param name="toAccount">Identifies the account that will be credited</param>
        /// <param name="transferAmount">The amount of the transfer</param>
        public void TransferTo(Account toAccount, double transferAmount)
        {
            if (null == toAccount) throw new PsAccountManagerException(PsAccountManagerException.NullToAccount);
            if (transferAmount > Balance) throw new PsAccountManagerException(PsAccountManagerException.TransferAmountExceedsAvailable);

            Balance -= transferAmount;
            toAccount.Balance += transferAmount;
        }
    }
}
